package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.Achievement;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 成就列表适配器
 */
public class AchievementAdapter extends RecyclerView.Adapter<AchievementAdapter.AchievementViewHolder> {
    
    private final Context context;
    private final List<Achievement> achievements;
    private final SimpleDateFormat dateFormat;
    
    public AchievementAdapter(Context context, List<Achievement> achievements) {
        this.context = context;
        this.achievements = achievements;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    }
    
    @NonNull
    @Override
    public AchievementViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_achievement, parent, false);
        return new AchievementViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull AchievementViewHolder holder, int position) {
        Achievement achievement = achievements.get(position);
        holder.bind(achievement);
    }
    
    @Override
    public int getItemCount() {
        return achievements.size();
    }
    
    /**
     * 成就ViewHolder
     */
    class AchievementViewHolder extends RecyclerView.ViewHolder {
        private final CardView cardView;
        private final ImageView ivIcon;
        private final TextView tvName;
        private final TextView tvLevel;
        private final ImageView ivStatus;
        private final TextView tvDescription;
        private final ProgressBar progressBar;
        private final TextView tvProgress;
        private final TextView tvReward;
        private final TextView tvUnlockTime;
        
        public AchievementViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = (CardView) itemView;
            ivIcon = itemView.findViewById(R.id.iv_achievement_icon);
            tvName = itemView.findViewById(R.id.tv_achievement_name);
            tvLevel = itemView.findViewById(R.id.tv_achievement_level);
            ivStatus = itemView.findViewById(R.id.iv_achievement_status);
            tvDescription = itemView.findViewById(R.id.tv_achievement_description);
            progressBar = itemView.findViewById(R.id.progress_achievement);
            tvProgress = itemView.findViewById(R.id.tv_achievement_progress);
            tvReward = itemView.findViewById(R.id.tv_achievement_reward);
            tvUnlockTime = itemView.findViewById(R.id.tv_unlock_time);
        }
        
        public void bind(Achievement achievement) {
            // 设置成就图标
            ivIcon.setImageResource(R.drawable.ic_achievement_placeholder);
            
            // 设置成就名称
            tvName.setText(achievement.getName());
            
            // 设置成就等级
            String levelText;
            int levelColor;
            switch (achievement.getLevel()) {
                case Achievement.LEVEL_BRONZE:
                    levelText = "铜级";
                    levelColor = 0xFFCD7F32; // 铜色
                    break;
                case Achievement.LEVEL_SILVER:
                    levelText = "银级";
                    levelColor = 0xFFC0C0C0; // 银色
                    break;
                case Achievement.LEVEL_GOLD:
                    levelText = "金级";
                    levelColor = 0xFFFFD700; // 金色
                    break;
                case Achievement.LEVEL_DIAMOND:
                    levelText = "钻石";
                    levelColor = 0xFFB9F2FF; // 钻石色
                    break;
                default:
                    levelText = "基础";
                    levelColor = 0xFF757575; // 灰色
                    break;
            }
            tvLevel.setText(levelText);
            tvLevel.setTextColor(levelColor);
            
            // 设置成就描述
            tvDescription.setText(achievement.getDescription());
            
            // 设置成就进度
            int progress = (achievement.getTargetValue() > 0)
                    ? (achievement.getCurrentValue() * 100 / achievement.getTargetValue())
                    : 0;
            progressBar.setProgress(Math.min(progress, 100));
            tvProgress.setText(String.format(Locale.getDefault(), "%d/%d", 
                    achievement.getCurrentValue(), achievement.getTargetValue()));
            
            // 设置成就奖励
            tvReward.setText(String.format(Locale.getDefault(), "奖励: ¥%.1f", achievement.getReward()));
            
            // 设置解锁状态和时间
            if (achievement.isUnlocked()) {
                // 已解锁状态
                ivStatus.setImageResource(R.drawable.ic_achievement_completed);
                ivStatus.setVisibility(View.VISIBLE);
                
                // 设置解锁时间
                if (achievement.getUnlockTime() > 0) {
                    String unlockTime = dateFormat.format(new Date(achievement.getUnlockTime()));
                    tvUnlockTime.setText(String.format("解锁时间: %s", unlockTime));
                    tvUnlockTime.setVisibility(View.VISIBLE);
                } else {
                    tvUnlockTime.setVisibility(View.GONE);
                }
                
                // 设置已解锁样式
                cardView.setCardBackgroundColor(0xFFE8F5E9); // 浅绿色背景
                tvName.setTextColor(0xFF4CAF50); // 绿色文字
                progressBar.setProgressDrawable(context.getDrawable(R.drawable.progress_green));
            } else {
                // 未解锁状态
                ivStatus.setVisibility(View.GONE);
                tvUnlockTime.setVisibility(View.GONE);
                
                // 设置未解锁样式
                cardView.setCardBackgroundColor(0xFFFFFFFF); // 白色背景
                tvName.setTextColor(0xFF333333); // 默认文字颜色
                progressBar.setProgressDrawable(context.getDrawable(android.R.drawable.progress_horizontal));
            }
        }
    }
} 